﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HP.Cachalote.DataModels;
using HP.Cachalote.Services.Interfaces;
using HP.Cachalote.Services.Shared;
using HP.Cachalote.WPF.Model;
using HP.Cachalote.WPF.Shared;

namespace HP.Cachalote.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class EditNoteViewModel : MyViewModelBase
    {
        public const string NoteUpdatedMessageToken = "NoteUpdated";
        public const string RequestCloseEditNoteWindowMessageToken = "RequestCloseEditNoteWindowMessage";

        public int TodoItemID { get; set; }
        public string InitializedNote { get; set; }

        /// <summary>
        /// Initializes a new instance of the EditNoteViewModel class.
        /// </summary>
        public EditNoteViewModel(IDataService dataService)
            : base(dataService) { }

        /// <summary>
        /// The <see cref="TodoItemTitle" /> property's name.
        /// </summary>
        public const string TodoItemTitlePropertyName = "TodoItemTitle";

        private string _todoItemTitle = string.Empty;

        /// <summary>
        /// Sets and gets the TodoItemTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string TodoItemTitle
        {
            get
            {
                return _todoItemTitle;
            }

            set
            {
                if (_todoItemTitle == value)
                {
                    return;
                }

                RaisePropertyChanging(TodoItemTitlePropertyName);
                _todoItemTitle = value;
                RaisePropertyChanged(TodoItemTitlePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="Note" /> property's name.
        /// </summary>
        public const string NotePropertyName = "Note";

        private string _note = string.Empty;

        /// <summary>
        /// Sets and gets the Note property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string Note
        {
            get
            {
                return _note;
            }

            set
            {
                if (_note == value)
                {
                    return;
                }

                RaisePropertyChanging(NotePropertyName);
                _note = value;
                RaisePropertyChanged(NotePropertyName);
            }
        }

        private RelayCommand _saveCommand;

        /// <summary>
        /// Gets the SaveCommand.
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand
                    ?? (_saveCommand = new RelayCommand(ExecuteSaveCommand));
            }
        }

        private void ExecuteSaveCommand()
        {
            try
            {
                var todoItemService = ServiceFactory.CreateTodoItemService();
                var todoItem = todoItemService.Get(TodoItemID);
                Note = Note.Trim();

                if (todoItem != null && InitializedNote != Note)
                {
                    InitializedNote = Note;
                    todoItem.Note = Note;
                    todoItem.UpdateOn = GeneralUtilities.GetCurrentDateTime();
                    todoItemService.Update(todoItem);

                    // Send message to TodoItemTreeViewItemViewModel to change the Note.
                    Messenger.Default.Send<EditNoteViewModel>(this, NoteUpdatedMessageToken);
                }

                Messenger.Default.Send<int>(0, RequestCloseEditNoteWindowMessageToken);
            }
            catch (Exception ex)
            {
                RequestDisplayErrorMessage(ex.Message);
            }
        }

        private RelayCommand _cancelCommand;

        /// <summary>
        /// Gets the CancelCommand.
        /// </summary>
        public RelayCommand CancelCommand
        {
            get
            {
                return _cancelCommand
                    ?? (_cancelCommand = new RelayCommand(
                                          () =>
                                          {
                                              // Send message to EditNoteWindow to close the window.
                                              Messenger.Default.Send<int>(0, RequestCloseEditNoteWindowMessageToken);
                                          }));
            }
        }
    }
}